package com.gm.wholesale.constant;

import android.text.TextUtils;

import com.gm.commonlib.util.numberUtils.NumberUtils;

public enum Unit {
    //斤,公斤,克,千克,kg
    JING(1, "斤"),
    GONG_JING(2, "公斤"),
    KE(3, "克"),
    KG(4, "kg"),
    QIANKE(5, "千克"),
    LIANG(6,"两"),
    KE2(7,"g");
    private String message;
    private int type;

    Unit(int type, String desc) {
        this.type = type;
        this.message = desc;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public static Unit getByCode(int code) {
        for (Unit data : values()) {
            if (data.getType() == code) {
                return data;
            }
        }
        return Unit.KE;
    }

    public static Unit getByName(String name) {
        if (TextUtils.isEmpty(name)) {
            return null;
        }
        for (Unit data : values()) {
            if (data.getMessage().equals(name)) {
                return data;
            }
        }
        return null;
    }
    //分拣称是0.5kg的东西，我换成斤，称重变成0.25
    public static double getWeight(String goodUnit, String changeUnit, double weight) {
        try{
            if (TextUtils.isEmpty(goodUnit)
                    || TextUtils.isEmpty(changeUnit)) {
                return weight;
            }
            goodUnit = goodUnit.toLowerCase();
            if (goodUnit.equals(changeUnit)) {
                return weight;
            }
            changeUnit=changeUnit.toLowerCase();
            Unit unit = getByName(goodUnit);
            if (unit==null){
                return 0;
            }
            if (changeUnit.equals(Unit.JING.message)){ //换算成斤
                switch (unit){
                    case JING:
                        return weight;
                    case GONG_JING:
                    case KG:
                    case QIANKE:
                        return NumberUtils.mul(weight,2);
                    case KE:
                    case KE2:
                        return NumberUtils.div(weight,500,2);
                    case LIANG:
                        return NumberUtils.div(weight,10,2);
                }
            }
            else if (changeUnit.equals(Unit.GONG_JING.message)//换算成公斤
                    || changeUnit.equals(Unit.KG.message)
                    || changeUnit.equals(Unit.QIANKE.message)){

                switch (unit){
                    case JING:
                        return NumberUtils.div(weight,2,2);
                    case GONG_JING:
                    case KG:
                    case QIANKE:
                        return weight;
                    case KE:
                    case KE2:
                        return NumberUtils.div(weight,1000,2);
                    case LIANG:
                        return NumberUtils.div(weight,20,2);
                }
            }
            else if (changeUnit.equals(Unit.KE.message)
                    || changeUnit.equals(Unit.KE2.message)){//换成克
                switch (unit){
                    case JING:
                        return NumberUtils.mul(weight, 500);
                    case GONG_JING:
                    case KG:
                    case QIANKE:
                        return NumberUtils.mul(weight, 1000);
                    case KE:
                    case KE2:
                        return weight;
                    case LIANG:
                        return NumberUtils.mul(weight, 50);
                }
            }
            else if (changeUnit.equals(Unit.LIANG.message)){//换算成两
                switch (unit){
                    case JING:
                        return NumberUtils.mul(weight, 10);
                    case GONG_JING:
                    case KG:
                    case QIANKE:
                        return NumberUtils.mul(weight, 20);
                    case KE:
                    case KE2:
                        return NumberUtils.div(weight,50,2);
                    case LIANG:
                        return weight;
                }
            }
            return weight;
        }
        catch (Exception e){
            return 0;
        }


    }
}
